<?php
    /**
     * 套餐管理控制器
     * create by liuqi
     */
    namespace cash\Controller;

    use Think\Controller;

    class ProjectController extends ApibaseController
    {
        /**
         * 套餐管理列表
         * create by: liuqi
         * Date: 2018年3月6日11:36:31
         */
        public function index()
        {
            $page = I('get.page', 1);
            $perpage = 10;
            $url = [];
            $list = M('ProjectEntity')->where(['delete_at' => 0]);

            if (I('get.project_name')) {
                $list = $list->where('project_name LIKE "%' . trim(I('get.project_name')) . '%"');
                $url['project_name'] = trim(I('get.project_name'));
            }
            if (in_array(I('get.project_status'), [1, 2])) {
                $list = $list->where(['project_status' => I('get.project_status') - 1]);
                $url['project_status'] = I('get.project_status');
            }
            if (in_array(I('get.score_type'), [1, 2, 3, 4, 5])) {
                $list = $list->where(['score_type' => I('get.score_type')]);
                $url['score_type'] = I('get.score_type');
            }

            $copy = clone $list;
            $getData = $list->page($page, $perpage)->select();
            $this->assign('page', setPage($copy->count(), $perpage, $page, '?' . http_build_query($url)));
            $this->assign('getData', $getData);
            $this->display('project/index');
        }

        /**
         * jolly商品管理
         * create by: liuqi
         * Date: 2018-1-15 15:31:50
         */
        public function jolly()
        {
            $page = I('get.page', 1);
            $perpage = 10;
            $url = [];
            $list = M('JollyEntity')->where(['delete_at' => 0]);
            if(trim(I('get.jolly_name'))){
                $list = $list->where("jolly_name LIKE '%".trim(I('get.jolly_name'))."%'");
            }
            $copy = clone $list;
            $getData = $list->page($page, $perpage)->select();
            $this->assign('page', setPage($copy->count(), $perpage, $page, '?' . http_build_query($url)));
            $this->assign('getData', $getData);
            $this->display('project/jolly');
        }


        /**
         * 添加/修改 套餐
         */
//	public function addPro()
//	{
//		if(I('post.dosubmit')){
//			$post_img = I('post.cover_img');
//			if($post_img{0} == '/'){
//				$cover_img = uploadQiniu(UPLOAD_PATH.$post_img,'pf_pro');
//			}else{
//				$cover_img = $post_img;
//			}
//			$data = [
//				'project_name'	=> trim(I('post.project_name')),
//				'project_price'	=> trim(I('post.project_price')),
//				'project_desc'  => htmlspecialchars(trim(I('post.project_desc'))),
//				'score_type'	=> trim(I('post.score_type')),
////				'rebate'		=> trim(I('post.rebate')),
//				'project_status'=> trim(I('post.project_status')),
//				'cover_img'		=> $cover_img,
//				'update_at'		=> time()
//			];
//
//			if(I('post.project_id')){
//				$set = M('ProjectEntity')->where(['project_id'=>I('post.project_id')])->setField($data);
//				if($set){
//					$this->showtips('修改成功');
//				}
//			}
//			$data['add_at'] = time();
//			if(M('ProjectEntity')->add($data)){
//				$this->showtips('添加成功');
//			}
//		}elseif(I('get.project_id')){
//			$getData = M('ProjectEntity')->where(['project_id'=>I('get.project_id')])->find();
//			$this->assign('getData',$getData);
//		}
//		$this->display('project/add_pro');
//	}


        /**
         * 添加修改套餐
         * create by: liuqi
         * Date:2018-1-10 14:30:23
         */
        public function addPro()
        {
            if (I('post.dosubmit')) {
//			var_dump(I('post.'));exit;
                $post_img = I('post.cover_img');
                if ($post_img{0} == '/') {
                    $cover_img = uploadQiniu(UPLOAD_PATH . $post_img, 'pf_pro');
                } else {
                    $cover_img = $post_img;
                }
                $data = ['project_name' => trim(I('post.project_name')), 'project_price' => trim(I('post.project_price')), 'project_desc' => htmlspecialchars(trim(I('post.project_desc'))), 'score_type' => trim(I('post.score_type')), 'project_status' => trim(I('post.project_status')), 'content' => implode(';', I('post.cards_num')), 'cover_img' => $cover_img, 'update_at' => time()];

                if (I('post.project_id')) {
                    $set = M('ProjectEntity')->where(['project_id' => I('post.project_id')])->setField($data);
                    if ($set) {
                        $this->showtips('修改成功');
                    }
                }
                $data['add_at'] = time();
                if (M('ProjectEntity')->add($data)) {
                    $this->showtips('添加成功');
                }
            } elseif (I('get.project_id')) {
                $getData = M('ProjectEntity')->where(['project_id' => I('get.project_id')])->find();
                if ($getData['content']) {
                    $content = explode(';', $getData['content']);
                    if ($content) {
                        foreach ($content as $k => $val) {
                            $arr = explode(',', $val);
                            $content_data[$arr[0]] = $arr[1];
                        }
                    }
                    $getData['content'] = $content_data;
                }
                $this->assign('getData', $getData);
                $this->assign('is_add', 2);
            }

            //已有的项目 在select里不再显示
            if ($getData) {
                if ($getData['content']) {
                    $keys = implode(',', array_keys($getData['content']));
                    $acards = M('GoodsEntity')->where(['goods_id' => ['not in', $keys],'delete_at'=>0])->select();
                } else {
                    $acards = M('GoodsEntity')->where(['delete_at'=>0])->select();
                }
            } else {
                $acards = M('GoodsEntity')->where(['delete_at'=>0])->select();
            }
            $cards = M('GoodsEntity')->where(['delete_at'=>0])->select();
            $this->assign('cards', $cards);
            $this->assign('acards', $acards);

            $this->display('project/add_pro');
        }

        /**
         * 删除套餐
         * create by: liuqi
         * Date:2017-12-25 10:13:59
         */
        public function delPro()
        {
            if (I('get.project_id')) {
                $project_id = intval(I('get.project_id'));
                $del = M('ProjectEntity')->where(['project_id' => $project_id])->setField(['delete_at' => time(), 'update_at' => time(), 'project_status' => 0]);
                if ($del) {
                    $this->showtips('删除成功');
                }
            }
        }

        /**
         * 项目管理
         * create by: liuqi
         * Date:2017-12-25 17:07:19
         */
        public function goods()
        {
            $page = I('get.page', 1);
            $perpage = 10;
            $url = [];
            $list = M('GoodsEntity')->where(['delete_at' => 0]);

            if (I('get.goods_name')) {
                $list = $list->where('goods_name LIKE "%' . trim(I('get.goods_name')) . '%"');
                $url['goods_name'] = trim(I('get.goods_name'));
            }

            $copy = clone $list;
            $getData = $list->page($page, $perpage)->select();
            $this->assign('page', setPage($copy->count(), $perpage, $page, '?' . http_build_query($url)));
            $this->assign('getData', $getData);
            $this->display('project/goods');
        }

        /**
         * 添加/修改 项目
         * create by: liuqi
         * Date:2017-12-26 09:30:39
         */
        public function addGoods()
        {
            if (I('post.dosubmit')) {
                $data = ['goods_name' => trim(I('post.goods_name')), 'goods_desc' => htmlspecialchars(trim(I('post.goods_desc'))), 'update_at' => time()];
                if (I('post.goods_id')) {
                    $set = M('GoodsEntity')->where(['goods_id' => I('post.goods_id')])->setField($data);
                    if ($set) {
                        $this->showtips('修改成功');
                    }
                }
                $exists = M('GoodsEntity')->where(['goods_name' => trim(I('post.goods_name')),'delete_at'=>0])->find();
                if ($exists) {
                    $this->showtips('该项目已存在，请重新录入');
                }
                $data['add_at'] = time();
                if (M('GoodsEntity')->add($data)) {
                    $this->showtips('添加成功');
                }
            } elseif (I('get.goods_id')) {
                $getData = M('GoodsEntity')->where(['goods_id' => I('get.goods_id')])->find();
                $this->assign('getData', $getData);
            }
            $this->display('project/add_goods');
        }

        /**
         * 查看套餐的具体详情
         * create by: liuqi
         * Date:2017-12-27 11:49:28
         */
        public function getProjectDetail()
        {
            if (I('get.order_id')) {
                $data = M('CardEntity')->where(['order_id' => I('get.order_id')])->select();
                $this->assign('getData', $data);
                $this->display('project/project_detail');
            }
        }

        /**
         * 增加/修改jolly商品
         * create by: liuqi
         * Date: 2018-1-2 10:35:07
         */
        public function addJolly()
        {
            if (I('post.dosubmit')) {
                $detail_img = I('post.detail_img');
                $myArr = [];
                foreach ($detail_img as $k => $v) {
                    if ($v{0} == '/') {
                        $d_img = uploadQiniu(UPLOAD_PATH . $v, 'pf_jolly_detail');
                    } else {
                        $d_img = $v;
                    }
                    $myArr[] = $d_img;
                }

                $face = I('post.face_img');
                if ($face{0} == '/') {
                    $face_img = uploadQiniu(UPLOAD_PATH . $face, 'pf_jolly');
                } else {
                    $face_img = $face;
                }
                $data = [
                    'jolly_name'       => trim(I('post.jolly_name')),
//                    'score_type'       => trim(I('post.score_type')),
                    'jolly_price'      => trim(I('post.jolly_price')),
                    'jolly_desc'       => trim(I('post.jolly_desc')),
                    'jolly_face_img'   => $face_img,
                    'jolly_detail_img' => implode(',', $myArr)
                ];

                if (I('post.jolly_id')) {
                    $set = M('JollyEntity')->where(['jolly_id' => I('post.jolly_id')])->setField($data);
                    if ($set) {
                        $this->showtips('修改成功');
                    }
                }
                $data['add_at'] = time();
                if (M('JollyEntity')->add($data)) {
                    $this->showtips('添加成功');
                }
            } elseif (I('get.jolly_id')) {
                $getData = M('JollyEntity')->where(['jolly_id' => I('get.jolly_id')])->find();
                $getData['jolly_detail_img'] = explode(',', $getData['jolly_detail_img']);
                $this->assign('getData', $getData);
            }

            $this->display('project/add_jolly');
        }

        /**
         * 删除jolly商品
         * create by: liuqi
         * Date:2017-12-30 15:57:03
         */
        public function delJolly()
        {
            if (I('get.jolly_id')) {
                $del = M('JollyEntity')->where(['jolly_id' => I('get.jolly_id')])->setField(['delete_at' => time()]);
                $this->showtips('删除成功');
            }
        }

        /**
         * 删除项目
         * create by: liuqi
         * Date:2018-1-9 10:37:28
         */
        public function delGoods()
        {
            if(I('get.goods_id')){
                $del = M('GoodsEntity')->where(['goods_id'=>intval(I('get.goods_id'))])->setField(['update_at'=>time(),'delete_at'=>time()]);
                if($del){
                    $this->showtips('删除成功');
                }
            }
        }

        /**
         * jolly订单管理
         * create by: liuqi
         * Date:2017-12-30 16:00:03
         */
        public function jollyOrder()
        {
            $page = I('get.page', 1);
            $perpage = 10;
            $url = [];
            $model = M('JollyOrder jo')->join('pf_user_info ui ON jo.user_id = ui.user_id')->field('jo.*,ui.user_mobile as umobile,ui.user_name as uname');

            if (trim(I('get.user_name'))) {
                $model = $model->where(['jo.user_name' => trim(I('get.user_name'))]);
                $url['user_name'] = trim(I('get.user_name'));
            }
            if (trim(I('get.user_mobile'))) {
                $model = $model->where(['jo.user_mobile' => trim(I('get.user_mobile'))]);
                $url['user_mobile'] = trim(I('get.user_mobile'));
            }
            if(in_array(I('get.order_status'),['0','1','2','-1'])){
                $model = $model->where(['jo.order_status' => trim(I('get.order_status'))]);
                $url['order_status'] = trim(I('get.order_status'));
            }
            $copy = clone $model;
            $getData = $model->page($page, $perpage)->order(['jo.add_at'=>'desc'])->select();
            $JollyEntity = M('JollyEntity')->select();
            $jolly_info = [];
            foreach($JollyEntity as $k1=>$v1){
                $jolly_info[$v1['jolly_id']] = $v1['jolly_name'];
            }
            foreach($getData as $k=>$v){
                $getData[$k]['jolly_name'] =$jolly_info[$v['jolly_id']];
            }
            $this->assign('page', setPage($copy->count(), $perpage, $page, '?' . http_build_query($url)));
            $this->assign('getData', $getData);
            $this->display('project/jolly_order');
        }

        /**
         * 订单确定发货
         * create by: liuqi
         * Date:2017-12-30 16:09:22
         */
        public function sendOrder()
        {
            if (I('post.dosubmit')) {
                if (I('post.id')) {
                    $JollyOrder = M('JollyOrder');
                    $data = $JollyOrder->where(['order_status' => 0, 'id' => I('post.id')])->find();
                    if ($data) {
                        $update = $JollyOrder->where(['order_status' => 0, 'id' => I('post.id')])->setField(['order_status' => 1, 'send_at' => time(), 'express_com' => trim(I('post.express_com')), 'express_num' => trim(I('post.express_num')), 'remark' => trim(I('post.remark'))]);
                        if ($update) {
                            $this->showtips('发货成功');
                        }
                        $this->showtips('发货失败[0]');
                    }
                    $this->showtips('发货失败[1]');
                }
            }
            if (I('get.id')) {
                $getData = M('JollyOrder')->where(['id' => I('get.id')])->find();
                $this->assign('getData', $getData);
                $this->display('project/send_order');
            }
        }

        /**
         * 修改发货详情
         * create by: liuqi
         * Date:2018-1-3 15:36:20
         */
        public function editOrder()
        {
            if (I('post.dosubmit')) {
                if (I('post.id')) {
                    $JollyOrder = M('JollyOrder');
                    $data = $JollyOrder->where(['order_status' => 1, 'id' => I('post.id')])->find();
                    if ($data) {
                        $update = $JollyOrder->where(['order_status' => 1, 'id' => I('post.id')])->setField(['express_com' => trim(I('post.express_com')), 'express_num' => trim(I('post.express_num')), 'remark' => trim(I('post.remark'))]);
                        if ($update) {
                            $this->showtips('修改成功');
                        }
                        $this->showtips('修改失败[0]');
                    }
                    $this->showtips('修改失败[1]');
                }
            }
            if (I('get.id')) {
                $getData = M('JollyOrder')->where(['id' => I('get.id')])->find();
                $this->assign('getData', $getData);
                $this->display('project/send_detail');
            }
        }

        /**
         * 取消jolly订单
         * create by: liuqi
         * Date:2018-1-9 09:16:27
         */
        public function cancelJolly()
        {
            $id = intval(I('get.id'));
            if($id){
                $JollyOrder = M('JollyOrder');
                $UserInfo = M('UserInfo');
                $JollyOrder->startTrans();
                $UserInfo->startTrans();
                $data = $JollyOrder->lock(true)->where(['id'=>$id,'order_status'=>0])->find();
                if($data){
                    $cancel = $JollyOrder->where(['id'=>$id])->setField(['order_status'=>-1]);
                    if($cancel){
                        $type = M('JollyEntity')->where(['jolly_id'=>$data['jolly_id']])->getField('score_type');
                        $total_money = $data['total_price'];
                        $user_id = intval($data['user_id']);
                        if($type == 1){
                            $score_type = 'ordinary_score';
                        }elseif($type == 2){
                            $score_type = 'platina_score';
                        }elseif($type == 3){
                            $score_type = 'diamonds_score';
                        }else{
                            $JollyOrder->rollback();
                            $this->showtips('订单错误[0]');
                        }
                        $is_back = $UserInfo->lock(true)->where(['user_id'=>$user_id])->setInc($score_type,$total_money);
                        if($is_back){
                            $JollyOrder->commit();
                            $UserInfo->commit();
                            $this->showtips('取消订单成功');
                        }else{
                            $JollyOrder->rollback();
                            $UserInfo->rollback();
                            $this->showtips('取消订单失败');
                        }
                    }else{
                        $this->showtips('取消订单失败[1]');
                    }
                }else{
                    $this->showtips('订单有误');
                }
            }
        }
    }